package com.zhangshitong.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.zhangshitong.exception.ParamException;
import com.zhangshitong.exception.PermissionException;

@Slf4j
public class SpringExceptionResolver implements HandlerExceptionResolver{

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		String url = request.getRequestURL().toString();
		ModelAndView mv;
		String defaultMsg = "System error";
		
		// .json ,.page
		//这里我们要求项目中所有请求json数据，都使用.json结尾
		if(url.endsWith(".json")){
			if(ex instanceof PermissionException || ex instanceof ParamException){
				JsonData result = JsonData.fail(ex.getMessage());
				mv = new ModelAndView("jsonView", result.toMap());//使异常返回的形式与正常返回的数据结构一致
			}else{
				log.error("unknown json exception,url: "+ url,ex);
				JsonData result = JsonData.fail(defaultMsg);
				mv = new ModelAndView("jsonView", result.toMap());//使异常返回的形式与正常返回的数据结构一致
			}
		}else if(url.endsWith(".page")){//这里我们要求项目中所有请求page页面,都使用.page结尾
			log.error("unknown page exception,url: "+ url,ex);
			JsonData result = JsonData.fail(defaultMsg);
			mv = new ModelAndView("exception", result.toMap());//会返回到exception.jsp
		}else{
			log.error("unknown page exception,url: "+ url,ex);
			JsonData result = JsonData.fail(defaultMsg);
			mv = new ModelAndView("jsonView", result.toMap());//
		}
		return mv;
	}

}
